package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Goods;
import com.itheima.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Create By Awen on 2021/9/28
 *
 * @author Awen
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    @PostMapping
    public Result add(@RequestBody Goods goods) {
        return goodsService.add(goods);
    }

    @RequestMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        return goodsService.findById(id);
    }

    @RequestMapping("/search/{curPage}/{size}")
    public Result findPage(@PathVariable("curPage") Integer page, @PathVariable("size") Integer size) {

        return goodsService.findPage(page, size);
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Goods goods) {
        boolean b = goodsService.updateById(goods);
        if (b) {
            return Result.builder().flag(true).code(StatusCode.OK).message("修改成功").build();
        }

        return Result.builder().flag(false).code(StatusCode.OK).message("修改失败").build();
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        return goodsService.deleteById(id);
    }


}
